﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.Composite.Extensions;
using Microsoft.Practices.Composite.Regions;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.Infrastructure.Interface.Documents;
using Jbe.TestSuite.TestDevices.SignalVisualizer.Properties;
using System.Collections.Specialized;

namespace Jbe.TestSuite.TestDevices.SignalVisualizer.Applications
{
    internal class ModuleController
    {
        private readonly IApplicationContainer container;
        private readonly IRegionManager regionManager;
        private readonly IUIElementCreationService uiElementCreationService;
        private DelegateCommand closeCommand;
        private VisualizerPresentationModel presentationModel;


        public ModuleController(IApplicationContainer container, IRegionManager regionManager, IUIElementCreationService uiElementCreationService)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.uiElementCreationService = uiElementCreationService;
        }


        public void Initialize()
        {
            closeCommand = new DelegateCommand(Close);
            DocumentCommands.Close.RegisterCommand(closeCommand);

            // TODO: The order of the ui elements in the toolbar are the same as module loading. However, the module loading
            //       order is not specified.
            IMenuItem menuItem = uiElementCreationService.CreateMenuItem(
                Resources.VisualizerMenu, Resources.SignalVisualizer.ToBitmap());
            menuItem.Click += ShowVisualizerClick;
            regionManager.Regions[ShellRegionNames.ViewMenuItems].Add(menuItem);

            IToolBarItem toolBarItem = uiElementCreationService.CreateToolBarItem(
                Resources.Visualizer, Resources.SignalVisualizer.ToBitmap());
            toolBarItem.Click += ShowVisualizerClick;
            IToolBar toolBar = uiElementCreationService.CreateToolBar(toolBarItem);
            regionManager.Regions[ShellRegionNames.ToolBarTray].Add(toolBar);
        }

        private void ShowVisualizerClick(object sender, EventArgs e)
        {
            if (presentationModel == null)
            {
                presentationModel = container.Resolve<VisualizerPresentationModel>();
                
                regionManager.Regions[ShellRegionNames.ContentRegion].ActiveViews.CollectionChanged += ActiveViewsCollectionChanged;
                regionManager.Regions[ShellRegionNames.ContentRegion].Add(presentationModel.View);
            }
            regionManager.Regions[ShellRegionNames.ContentRegion].Activate(presentationModel.View);
        }

        private void ActiveViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Contains(presentationModel.View)) { closeCommand.IsActive = true; }
            else if (e.OldItems != null && e.OldItems.Contains(presentationModel.View)) { closeCommand.IsActive = false; }
        }

        private void Close()
        {
            regionManager.Regions[ShellRegionNames.ContentRegion].Remove(presentationModel.View);
            regionManager.Regions[ShellRegionNames.ContentRegion].ActiveViews.CollectionChanged -= ActiveViewsCollectionChanged;
            
            presentationModel = null;
        }
    }
}
